<?php
declare (strict_types=1);

namespace app\admin\model\advertiser;

use app\admin\model\base\UserOfficial;
use app\admin\model\base\UserProgram;
use app\BaseModel;

/**
 * 广告聚合平台日志记录表
 */
class AdvertiserThirdLog extends BaseModel
{

    /**
     * 写入小程序聚合平台日志
     * @param int $user_id
     * @param int $third_id
     * @param string $mac_id
     * @return int
     */
    public static function writeMiniprogramLog(int $user_id, int $third_id, string $mac_id): int
    {
        //查询当前平台是否存在
        $where = ['id' => $third_id];
        $Third = AdvertiserThird::findOne($where);
        if ($Third) {
            //写入日志
            $temp_log = [
                'third_id' => $third_id,
                'user_id' => $user_id,
                'appid' => $Third['mini_appid'],
                'link' => $Third['mini_link'],
                'mac_id' => $mac_id,
                'create_time' => time(),
            ];
            self::insertGetId($temp_log);
            //第一次写入小程序
            if ($Third['type'] == 'Miniprogram' and $user_id and $Third['mini_appid']) {
                $bool = UserProgram::oneLogSave($user_id, $Third['mini_appid'], $third_id, $Third['mini_link']);
                return intval($bool);
            }
        }
        return 2;
    }

    /**
     * 写入公众号聚合平台日志
     * @param int $user_id
     * @param int $third_id
     * @param string $appid
     * @param string $mac_id
     * @return int
     */
    public static function writeOfficialAccountLog(int $user_id, int $third_id, string $appid, string $mac_id): int
    {
        //查询当前平台是否存在
        $where = ['id' => $third_id];
        $Third = AdvertiserThird::findOne($where);
        if ($Third) {
            //写入日志
            $temp_log = [
                'third_id' => $third_id,
                'user_id' => $user_id,
                'appid' => $appid,
                'link' => '',
                'mac_id' => $mac_id,
                'create_time' => time(),
            ];
            self::insertGetId($temp_log);
            //第一次写入小程序
            if ($Third['type'] == 'OfficialAccount' and $user_id) {
                $bool = UserOfficial::oneLogSave($user_id, $appid, $third_id);
                return intval($bool);
            }
        }
        return 2;
    }


}




